﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;

public partial class admin_Member_SendMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
        LoadMail();
    }

    private void LoadMail()
    {
      UserBLL userData = new UserBLL();
      EmailBLL emailData = new EmailBLL();

      var listEmail = emailData.GetAll();
      var listUser = userData.GetAll(true);
      foreach (var item in listUser)
      {
        ListItem o = new ListItem("<b>" + item.Email + " (" + item.Name + ")</b>", item.Email);
        checkListEmail.Items.Add(o);
      }

      foreach (var item in listEmail)
      {
        ListItem o = new ListItem(item.Email1, item.Email1);
        checkListEmail.Items.Add(o);
      }
    }
    protected void checkAll_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < checkListEmail.Items.Count; i++)
      {
        checkListEmail.Items[i].Selected = true;
      }
    }
    protected void uncheckAll_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < checkListEmail.Items.Count; i++)
      {
        checkListEmail.Items[i].Selected = false;
      }
    }
    protected void checkMember_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < checkListEmail.Items.Count; i++)
      {
        if (checkListEmail.Items[i].Text.Contains("("))
        {
          checkListEmail.Items[i].Selected = true;
        }
      }
    }

    protected void checkEmail_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < checkListEmail.Items.Count; i++)
      {
        if (!checkListEmail.Items[i].Text.Contains("("))
        {
          checkListEmail.Items[i].Selected = true;
        }
      }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
      List<string> listEmail = new List<string>();
      for (int i = 0; i < checkListEmail.Items.Count; i++)
      {
        if (checkListEmail.Items[i].Selected)
        {
          listEmail.Add(checkListEmail.Items[i].Value);
        }
      }
      if (listEmail.Count == 0)
      {
        Myfunction.ThongBao(ltThongBao, "Phải chọn ít nhất 1 Email để gửi !");
        return;
      }
      if (txtTitle.Text == "")
      {
        Myfunction.ThongBao(ltThongBao, "Nhập tiêu đề");
        return;
      }
      if (FCKeditor1.Value == "")
      {
        Myfunction.ThongBao(ltThongBao, "Nhập nội dung");
        return;
      }
      string toAddress = "";
      foreach (var item in listEmail)
      {
        toAddress += item + ",";
      }
      toAddress = toAddress.Substring(0, toAddress.Length - 1);
      MyConfig.LoadMail();
      Myfunction.SendMail(MyConfig.MailUserName, MyConfig.MailPassWord, "Admin - DoxachtayUSA", toAddress, txtTitle.Text, FCKeditor1.Value);
    }
}